﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcCommands.CApAWCMfgCache
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using ActiveWorkspaceHosting;
using EngineeringInternalExtension;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Tecnomatix.Engineering;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands.TcCommands;

internal class CApAWCMfgCache
{
  private CApAWCMfgTcServices m_mfgServices;
  private Dictionary<ITxObject, string> m_previewedMfgToUid = new Dictionary<ITxObject, string>();
  private ArrayList m_mfgs = new ArrayList();
  private bool m_initialized;
  private bool m_ignoreVisiblityChange;

  public event EventHandler<CApAWCMfgVisibilityUpdateEventArgs> MfgVisibilityUpdate;

  public CApAWCMfgCache(CApAWCMfgTcServices mfgServices) => this.m_mfgServices = mfgServices;

  public bool IgnoreVisiblityChange
  {
    get => this.m_ignoreVisiblityChange;
    set => this.m_ignoreVisiblityChange = value;
  }

  public bool HasPreviewdMfgs => this.m_previewedMfgToUid.Count > 0;

  public void ClearPreviewedMfgs()
  {
    if (!this.m_initialized)
      return;
    this.m_previewedMfgToUid.Clear();
  }

  public void ClearMfgs()
  {
    if (!this.m_initialized)
      return;
    for (int index = this.m_mfgs.Count - 1; index >= 0; --index)
    {
      TxMfgFeature mfg = this.m_mfgs[index] as TxMfgFeature;
      if (TxMfgFeature.op_Inequality(mfg, (ITxObject) null))
        this.Remove(mfg);
    }
  }

  public void Clear()
  {
    this.ClearPreviewedMfgs();
    this.ClearMfgs();
  }

  public void Initialize()
  {
    if (this.m_initialized)
      return;
    this.m_initialized = true;
    TxTypeFilter txTypeFilter = new TxTypeFilter();
    txTypeFilter.AddIncludedType(typeof (TxMfgFeature));
    foreach (ITxObject allDescendant in (Collection<ITxObject>) TxApplication.ActiveDocument.MfgRoot.GetAllDescendants((ITxTypeFilter) txTypeFilter))
    {
      TxMfgFeature mfg = allDescendant as TxMfgFeature;
      if (TxMfgFeature.op_Inequality(mfg, (ITxObject) null))
        this.Add(mfg);
    }
  }

  public void Add(TxMfgFeature mfg)
  {
    if (!this.m_initialized || this.m_mfgs.Contains((object) mfg))
      return;
    this.m_mfgs.Add((object) mfg);
    mfg.Deleted += new TxObject_DeletedEventHandler(this.onDeleted);
    mfg.VisibilitySet += new TxDisplayableObject_VisibilitySetEventHandler(this.OnVisibilitySet);
  }

  private void Remove(TxMfgFeature mfg)
  {
    if (!this.m_mfgs.Contains((object) mfg))
      return;
    mfg.Deleted -= new TxObject_DeletedEventHandler(this.onDeleted);
    mfg.VisibilitySet -= new TxDisplayableObject_VisibilitySetEventHandler(this.OnVisibilitySet);
    this.m_mfgs.Remove((object) mfg);
  }

  private void Display(TxMfgFeature mfg)
  {
    if (mfg.Visibility == null)
      return;
    mfg.Display();
  }

  private void Blank(TxMfgFeature mfg)
  {
    if (mfg.Visibility != null)
      return;
    mfg.Blank();
  }

  private void onDeleted(object sender, TxObject_DeletedEventArgs args)
  {
    TxMfgFeature txMfgFeature = sender as TxMfgFeature;
    if (!TxMfgFeature.op_Inequality(txMfgFeature, (ITxObject) null))
      return;
    this.Remove(txMfgFeature);
    this.m_previewedMfgToUid.Remove((ITxObject) txMfgFeature);
  }

  private void OnVisibilitySet(object sender, TxDisplayableObject_VisibilitySetEventArgs args)
  {
    TxMfgFeature mfg = sender as TxMfgFeature;
    if (!this.m_initialized || this.m_ignoreVisiblityChange || !TxMfgFeature.op_Inequality(mfg, (ITxObject) null))
      return;
    bool visible = mfg.Visibility == null;
    this.OnMfgVisibilityUpdate((ITxObject) mfg, visible);
  }

  private void OnMfgVisibilityUpdate(ITxObject mfg, bool visible)
  {
    if (this.MfgVisibilityUpdate == null)
      return;
    Dictionary<string, bool> visibilityDictionary = this.ToMfgVisibilityDictionary(mfg, visible);
    if (visibilityDictionary.Count <= 0)
      return;
    this.MfgVisibilityUpdate((object) this, new CApAWCMfgVisibilityUpdateEventArgs()
    {
      MfgsToUpdate = visibilityDictionary
    });
  }

  public Dictionary<string, bool> ToMfgVisibilityDictionary(ITxObject mfg, bool visible)
  {
    Dictionary<string, bool> visibilityDictionary = new Dictionary<string, bool>();
    TxObjectList txObjectList = new TxObjectList();
    ((Collection<ITxObject>) txObjectList).Add(mfg);
    List<string> previewedMfgsBomLineUids = this.GetPreviewedMfgsBomLineUids(txObjectList);
    if (previewedMfgsBomLineUids.Count == 0)
    {
      Dictionary<ITxPlanningObject, string> bomLineUids = this.m_mfgServices.GetBomLineUids(txObjectList);
      if (bomLineUids != null)
        previewedMfgsBomLineUids.AddRange((IEnumerable<string>) bomLineUids.Values);
    }
    if (previewedMfgsBomLineUids.Count == 1)
      visibilityDictionary.Add(previewedMfgsBomLineUids[0], visible);
    return visibilityDictionary;
  }

  public void UpdateVisibilityInTCMABeforePreviewIsCleared(List<TxMfgInfo> mfgsInfo)
  {
    if (!this.m_initialized || this.MfgVisibilityUpdate == null)
      return;
    List<string> list = mfgsInfo.Where<TxMfgInfo>((Func<TxMfgInfo, bool>) (x => x.UID != null)).Select<TxMfgInfo, string>((Func<TxMfgInfo, string>) (x => x.UID)).ToList<string>();
    Dictionary<string, bool> dictionary = new Dictionary<string, bool>();
    foreach (ITxObject key1 in this.m_previewedMfgToUid.Keys)
    {
      TxMfgFeature txMfgFeature = key1 as TxMfgFeature;
      if (TxMfgFeature.op_Inequality(txMfgFeature, (ITxObject) null))
      {
        string key2 = this.m_previewedMfgToUid[key1];
        bool flag = txMfgFeature.Visibility == 0;
        if (flag && !list.Contains(key2))
          dictionary.Add(key2, false);
        if (!flag && list.Contains(key2))
          dictionary.Add(key2, true);
      }
    }
    if (dictionary.Count <= 0)
      return;
    this.MfgVisibilityUpdate((object) this, new CApAWCMfgVisibilityUpdateEventArgs()
    {
      MfgsToUpdate = dictionary
    });
  }

  public void UpdateVisibilityInTCMAAfterLoad(IEnumerable<TxTcResultObjectDataEx> loadedMfgs)
  {
    if (!this.m_initialized || this.MfgVisibilityUpdate == null)
      return;
    Dictionary<string, TxMfgFeature> dictionary1 = loadedMfgs.ToDictionary<TxTcResultObjectDataEx, string, TxMfgFeature>((Func<TxTcResultObjectDataEx, string>) (di => di.Uid), (Func<TxTcResultObjectDataEx, TxMfgFeature>) (di => di.Object as TxMfgFeature));
    Dictionary<string, bool> dictionary2 = new Dictionary<string, bool>();
    foreach (string key in dictionary1.Keys)
    {
      TxMfgFeature txMfgFeature = dictionary1[key];
      if (TxMfgFeature.op_Inequality(txMfgFeature, (ITxObject) null) && txMfgFeature.Visibility == null)
        dictionary2.Add(key, true);
    }
    if (dictionary2.Count <= 0)
      return;
    this.MfgVisibilityUpdate((object) this, new CApAWCMfgVisibilityUpdateEventArgs()
    {
      MfgsToUpdate = dictionary2
    });
  }

  public void UpdateVisibilityInPSAfterPreview(
    IEnumerable<TxTcResultObjectDataEx> existingMfgs,
    IEnumerable<ITxMfgInfo> updatedMfgs)
  {
    Dictionary<string, TxMfgFeature> dictionary1 = existingMfgs.ToDictionary<TxTcResultObjectDataEx, string, TxMfgFeature>((Func<TxTcResultObjectDataEx, string>) (di => di.Uid), (Func<TxTcResultObjectDataEx, TxMfgFeature>) (di => di.Object as TxMfgFeature));
    Dictionary<string, bool> dictionary2 = updatedMfgs.Cast<TxMfgVisibilityUpdateInfo>().ToDictionary<TxMfgVisibilityUpdateInfo, string, bool>((Func<TxMfgVisibilityUpdateInfo, string>) (di => di.UID), (Func<TxMfgVisibilityUpdateInfo, bool>) (di => di.Visible));
    foreach (string key in dictionary1.Keys)
    {
      TxMfgFeature mfg = dictionary1[key];
      if (TxMfgFeature.op_Inequality(mfg, (ITxObject) null) && dictionary2.ContainsKey(key))
      {
        if (dictionary2[key])
          this.Display(mfg);
        else
          this.Blank(mfg);
      }
    }
  }

  public void AddPreviewedMfgBomLineUid(TxTcResultObjectDataEx resultObject)
  {
    if (!resultObject.Object.IsTransient || this.m_previewedMfgToUid.ContainsKey(resultObject.Object))
      return;
    this.m_previewedMfgToUid.Add(resultObject.Object, resultObject.Uid);
  }

  public List<string> GetPreviewedMfgsBomLineUids(TxObjectList mfgs)
  {
    List<string> previewedMfgsBomLineUids = new List<string>();
    foreach (ITxObject mfg in (Collection<ITxObject>) mfgs)
    {
      if (this.m_previewedMfgToUid.ContainsKey(mfg))
        previewedMfgsBomLineUids.Add(this.m_previewedMfgToUid[mfg]);
    }
    return previewedMfgsBomLineUids;
  }
}
